package com.corn.juc.aqs;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2021/1/12 11:52
 */

public class AQSDebugDemo {

    public static void main(String[] args) throws InterruptedException {
        // 非公平锁
        final ReentrantLock lock = new ReentrantLock();

        new Thread("A") {
            @SneakyThrows
            @Override
            public void run() {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + "\t enter!");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread().getName() + "\t out!");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }

            }
        }.start();
        TimeUnit.SECONDS.sleep(1);
        new Thread("B") {
            @SneakyThrows
            @Override
            public void run() {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + "\t enter!");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread().getName() + "\t out!");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }

            }
        }.start();

        TimeUnit.SECONDS.sleep(1);
        new Thread("C") {
            @SneakyThrows
            @Override
            public void run() {
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + "\t enter!");
                    System.out.println(Thread.currentThread().getName() + "\t out!");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }.start();
    }
}
